package cn.ulc.socket.common.config;

import cn.hutool.core.date.DateUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;
import java.util.Date;

@Configuration
public class DataSourceConfig {

	private static final Log log = LogFactory.get();

	@Bean(name = "dataSource")
	public DataSource datasource(Environment env) {
		HikariDataSource ds = new HikariDataSource();
		try {
			ds.setJdbcUrl(env.getProperty("spring.datasource.url"));
			ds.setUsername(env.getProperty("spring.datasource.username"));
			ds.setPassword(env.getProperty("spring.datasource.password"));
//			ds.setDriverClassName(driverClassName);
//			ds.setInitializationFailTimeout(initialSize);
//			ds.setMinimumIdle(minIdle);
//			ds.setMaximumPoolSize(macActive);
//			ds.setMaxLifetime(maxWait);
//			ds.setTime(timeBetweenEvictionRunsMillis);
//			ds.setId(minEvictableIdleTimeMillis);
//			ds.setValidationQuery(validationQuery);
//			ds.setTestWhileIdle(testWhileIdle);
//			ds.setTestOnBorrow(testOnBorrow);
//			ds.setTestOnReturn(testOnReturn);
//			ds.setPoolPreparedStatements(poolPreparedStatements);
//			ds.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
//			ds.setFilters(filters);
		}catch (Exception e) {
			log.error("初始化数据库异常{}，时间为{}",e.getMessage(), DateUtil.format(new Date(),"yyy-MM-dd hh:mm:ss"));
		}
		return ds;
	}

//	@Bean
//	public FilterRegistrationBean filterRegistrationBean() {
//		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
//		filterRegistrationBean.setFilter(new WebStatFilter());
//		filterRegistrationBean.addUrlPatterns("/*");
//		filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
//		return filterRegistrationBean;
//	}
}



